local sk__dengji = fk.CreateSkill {

  name = "sk__dengji",

  tags = { Skill.Wake, },

}



sk__dengji:addEffect(fk.EventPhaseStart, {
  name = "sk__dengji",
  frequency = Skill.Wake,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__dengji.name) and
      player.phase == Player.Start and
      player:usedSkillTimes(sk__dengji.name, Player.HistoryGame) == 0
  end,
  can_wake = function(self, event, target, player, data)
    return #player:getPile("sk__godcaopi_chu") > 4 and
      #player:getPile("sk__godcaopi_chu") % 2 == 1
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = player:getPile("sk__godcaopi_chu")
    local red, black = #table.filter(cards, function(id)
      return Fk:getCardById(id).color == Card.Red
    end),
    #table.filter(cards, function(id)
      return Fk:getCardById(id).color == Card.Black
    end)
    room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonPrey, sk__dengji.name, nil, true, player.id)
    if player.dead then return end
    local skills = {}
    if black > red then
      table.insert(skills, "sk__jiquan")
    else
      table.insert(skills, "sk__renzheng")
    end
    local n = math.min(red, black)
    if n > 0 then
      local lord_skills = {}
      for _, g in ipairs(Fk:getAllGenerals()) do
        local skill = table.find(g:getSkillNameList(true), function(s)
          return Fk.skills[s]:hasTag(Skill.Lord)
        end)
        if skill then
          table.insert(lord_skills, skill)
        end
      end
      table.insertTableIfNeed(skills, table.random(lord_skills, n))
    end
    room:handleAddLoseSkills(player, skills, nil, true, false)
  end,
})

return sk__dengji